export function getCookie(key) {
    if (document.cookie.length > 0) {
        let start = document.cookie.indexOf(key + '=');
        if (start !== -1) {
            start = start + key.length + 1;
            let end = document.cookie.indexOf(';', start);
            if (end === -1) {
                end = document.cookie.length;
            }
            return decodeURI(document.cookie.substring(start, end));
        }
    }
    return ''
}

export function setCookie(cName, value, expireDays) {
    const exdate = new Date();
    exdate.setDate(exdate.getDate() + expireDays)
    document.cookie = cName + '=' + decodeURIComponent(value) +
        ((expireDays == null) ? '' : ';expires=' + exdate.toUTCString())
}

export function setCookieFromObject(object, expireDays) {
    for (let key in object) {
        console.log(key);
        console.log(object[key]);
        setCookie(key, object[key], expireDays);
    }
}

export function clearCookie() {
    //清空cookie
    const keys = document.cookie.match(/[^ =;]+(?==)/g);
    console.log(keys);
    if (keys) {
        for (let i = keys.length; i--;) {
            document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();//清除当前域名下的,例如：m.kevis.com
            document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();//清除当前域名下的，例如 .m.kevis.com
            document.cookie = keys[i] + '=0;path=/;domain=kevis.com;expires=' + new Date(0).toUTCString();//清除一级域名下的或指定的，例如 .kevis.com
        }
    }
    console.log('已清除cookie');
}